package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.Book;
import com.example.domain.Bookshelf;
import com.example.domain.Room;
import com.example.domain.Seat;
import com.example.dto.RoomAddDTO;
import com.example.dto.RoomUpdateDTO;
import com.example.dto.SeatUpdateDTO;
import com.example.mapper.BookshelfMapper;
import com.example.mapper.RoomMapper;
import com.example.mapper.SeatMapper;
import com.example.service.RoomService;
import com.example.util.Result;
import com.example.vo.RoomVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author HP
* @description 针对表【room】的数据库操作Service实现
* @createDate 2025-08-24 20:53:08
*/
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room>
    implements RoomService{

    @Resource
    private RoomMapper roomMapper;

    @Resource
    private SeatMapper seatMapper;
    @Autowired
    private BookshelfMapper bookshelfMapper;

    @Override
    public Result addRoom(RoomAddDTO roomAddDTO) {
        // 检查是否存在
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", roomAddDTO.getName());
        queryWrapper.eq("library_id", roomAddDTO.getLibraryId());
        if (roomMapper.selectOne(queryWrapper) != null) {
            return Result.error("房间已存在");
        }
        Room room = new Room();
        BeanUtils.copyProperties(roomAddDTO, room);
        int result = roomMapper.insert(room);
        if (result != 1) {
            return Result.error("添加失败");
        }
        return Result.success();
    }

    @Override
    public Result getSeats(Long roomId) {
        List<Seat> seats = seatMapper.getSeatsByRoomId(roomId);
        return Result.success(Map.of("seats", seats));
    }

    @Override
    public Result updateRoom(RoomUpdateDTO roomUpdateDTO) {
        // 检查是否存在
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", roomUpdateDTO.getId());
        Room room = roomMapper.selectOne(queryWrapper);
        if (room == null) {
            return Result.error("房间不存在");
        }
        // 更新
        BeanUtils.copyProperties(roomUpdateDTO, room);
        room.setRoomStatus(1);
        int result = roomMapper.updateById(room);
        if (result != 1) {
            return Result.error("更新失败");
        }
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result deleteRoom(Long id) {
        // 检查是否存在
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Room room = roomMapper.selectOne(queryWrapper);
        if (room == null) {
            return Result.error("房间不存在");
        }
        int result = roomMapper.deleteById(id);
        if (result != 1) {
            return Result.error("删除失败");
        }
        if(room.getRoomType() == 1){
            // 删除座位
            QueryWrapper<Seat> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("studyRoomId", id);
            seatMapper.delete(queryWrapper1);
            // 删除书架
            QueryWrapper<Bookshelf> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("roomId", id);
            bookshelfMapper.delete(queryWrapper2);
        }
        return Result.success();
    }

    @Override
    public Result getAll() {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_status", 1);
        queryWrapper.eq("room_type", 0);
        List<Room> rooms = roomMapper.selectList(queryWrapper);
        List<RoomVO> roomVOS = rooms.stream().map(room -> {
            RoomVO roomVO = new RoomVO();
            BeanUtils.copyProperties(room, roomVO);
            String libraryName = roomMapper.selectById(room.getLibraryId()).getName();
            roomVO.setLibraryName(libraryName);
            return roomVO;
        }).toList();
        return Result.success(roomVOS);
    }

}




